# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from dao import models

# Create your views here.


def get_derction(request):
	derction_queryset = models.Dirction.objects.all().values('id', 'name')

	return derction_queryset


def get_classification(request, current_url, direction_id, classification_id, **q):
	if int(direction_id) == 0:  # 方向选中‘全部’时
		classification_queryset = models.Classification.objects.all().values('id', 'name')

		if int(classification_id) == 0:  # 视频过滤条件q，如果方向为全部，如果分类也为全部，条件只需状态为上线即可（status = 1）
			pass
		else:  #　否则增加条件．．．
			# q['classification_id'] = classification_id
			q['classfication_id__in'] = [classification_id, ]  #　由于分类id可以为多个，则用`__in`
	else:  # 方向选中某一个时
		obj_direction = models.Dirction.objects.get(id=int(direction_id))  # 查询某方向
		classification_queryset = obj_direction.classification.all().values('id', 'name')  # 查询某方向下的分类

		classification_id_list = list(map(lambda x: x['id'], classification_queryset))  # 分类id列表

		if int(classification_id) == 0:
			q['classfication__id__in'] = classification_id_list
		else:
			q['classfication__id__in'] = [classification_id, ]

			# 判断url中分类id是否存在于所查询的方向下（方向与分类为多对多关系）--方向非全部分类非全部时需处理
			# 若存在则不做处理，即还处于选中状态
			# 若不存在则将原选中的分类改为‘全部’，即方向id为0
			if int(classification_id) in classification_id_list:
				pass
			else:
				url_part_list = current_url.split('-')
				url_part_list[2] = str(0)
				current_url = '-'.join(url_part_list)

	return classification_queryset, current_url, q


def get_level(request, level_id, **q):
	# level_list = models.Video.level_choice  # 结果为元组
	level_list = list(map(lambda x: {'id': x[0], 'name': x[1]}, models.Video.level_choice))  # 使用map将元组构造为字典列表
	if int(level_id) == 0:
		pass
	else:
		q['level__in'] = [level_id, ]

	return level_list, q

def get_video(request, **q):
	video_queryset = models.Video.objects.filter(**q).values('title', 'summary', 'img', 'href')

	return video_queryset